<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use think\Cache;
use think\Config;
use think\Exception;
use XznsTools\cache\CacheManage;

class RecodeNote extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.app_recode_note';
    protected $cacheModel = 'recode.note';

    /**
     * 添加星盘笔记
     * @param $uid
     * @param $rid
     * @param $map
     * @return bool
     * @throws \Exception
     */
    public function SaveData($uid, $rid, $map)
    {
        try
        {
            $id = $this->insertGetId($map);
        }
        catch (\Exception $e)
        {
            return false;
        }
        CacheManage::getInstance()->setCacheConfig($this->cacheModel, $uid)->setListKeyType($rid)->rmCache('list');
        return $id;
    }

    /**
     * 更新星盘笔记
     * @param $id
     * @param $map
     * @return bool
     * @throws \Exception
     */
    public function UpdateData($id, $map)
    {
        $info = $this->getInfoById($id);
        if ($info)
        {
            try
            {
                $this->where(['uid' => $info['uid'], 'id' => $id])->update($map);
            }
            catch (\Exception $e)
            {
                return false;
            }
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $info['uid'])->setListKeyType($info['rid'])->rmCache('list');
            return true;
        }

        return false;
    }

    /**
     * 删除星盘笔记
     * @param $id
     * @param $map
     * @return bool
     * @throws \Exception
     */
    public function DelData($uid, $id)
    {
        $info = $this->getInfoById($id);
        if ($info)
        {
            try
            {
                $this->where(['id' => $id, 'uid' => $uid])->delete();
            }
            catch (\Exception $e)
            {
                return false;
            }
            Cache::rm(\app\common\model\RecodeNote::REDIS_KEY_IDS . $info['rid']);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $info['uid'])->setListKeyType($info['rid'])->rmCache('list');
            return true;
        }

        return false;
    }

    /**
     * 缓存获取星盘笔记数据
     * @param $uid
     * @return array|mixed
     * @throws \Exception
     */
    public function getNoteListFromCache($uid, $rid)
    {
        $page = input('page', 1);
        $limit = $this->pageLimit ?: Config::get('paginate.list_rows');

        $data = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel, $uid)
            ->setPage($page)
            ->setPageSize($limit)
            ->setSort('desc')
            ->setListKeyType($rid)
            ->getListPage(function ($ids = [], $isCache = true, $sortType = 'desc') use ($uid, $rid)
            {
                if ($isCache)
                { // 开启了缓存
                    return $this->getInfoByIdsInData($ids, [$uid, $rid], $sortType);
                } else
                { // 没开启缓存
                    return $this->getPaginateInData([$uid, $rid], $sortType);
                }
            }, 'id', [], 0, 'list', 'list_details');

        $arr = [];
        if (isset($data['data']) && $data['data'])
        {
            foreach ($data['data'] as $k => $v)
            {
                $arr[] = [
                    'id' => $v['id'],
                    'rid' => $v['rid'],
                    'uid' => $v['uid'],
                    'content' => $v['content'],
                    'create_time' => $v['create_time'] ? date('Y.m.d H:i', $v['create_time']) : '',
                    'last_time' => $v['last_time'] ? date('Y.m.d H:i', $v['last_time']) : '',
                    'from' => $v['from'],
                    'event_time' => $v['event_time'] ? date('Y.m.d H:i', $v['event_time']) : '',
                ];
            }
        }
        $data['data'] = $arr;
        return $data;
    }

    /**
     * 获取信息
     * @param $map
     * @param $sortType
     * @return array|void
     */
    public function getPaginateInData($map, $sortType)
    {
        try
        {
            $where = ['uid' => $map[0], 'rid' => $map[1]];

            $field = 'id,rid,uid,content,create_time,last_time,from,event_time';
            try
            {
                $data = $this->where($where)->field($field)->order('id', 'DESC')->paginate()->toArray();
            }
            catch (\Exception $e)
            {
                $data = [];
            }
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }


    /**
     * 从数据库批量获取星盘笔记信息
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        if ($ids)
        {
            $where['id'] = ['in', $ids];
        } else
        {
            $where = ['uid' => $type[0], 'rid' => $type[1]];
        }
        $field = 'id,rid,uid,content,create_time,last_time,from,event_time';
        try
        {
            $data = $this->where($where)->order('id', 'DESC')->column($field, 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }
        return $data;
    }
}
